function dGemricdrawfitmasks(protocols,basedir,varargin)

% This function is used to choose the images used for a dGemric analysis,
% and for drawing ROIs on these images. The masks are stored as an matlab
% structure in 'seriesdir'/Postprocessing/Mapping_Masks
% The location of the masks is also stored in a structure 'locations.mat'
% in the parent folder of the seriesdir, and later used for the T1 mapping.
% To redefine the locations of a mask, call with the masknr as
% varargin

% normal use: 
% drawcartilagemasks(protocols, basedir)
% if only used to select images for creating the location structure,
% drawing can be disabled:
% drawcartilagemasks(protocols, basedir, false)
% not drawing and force select folder of masknr:
% drawcartilagemasks(protocols, basedir, false, 2)

% Based on earlier work by Esther Bron
% Henk Smit, h.smit@erasmusmc.nl, 10-2011

cd(basedir)
maskparts={'_fem_wb','_fem_post', '_plat_wb'};
maskpartsnames={' femural weight bearing',' femural posterior', ' tibial'};

% Drawing switch
if nargin>2
    draw = varargin{1};
else
    draw = true;
end;


% Select 4 images for maskdrawing
for proto=1:protocols
    [file,dir]=uigetfile('*.dcm');
    files(proto).file=file;
    files(proto).dir=dir;
    filedir=[dir filesep file];
    drawdata(proto).info = dicominfo(filedir); 
    drawdata(proto).scan = double(dicomread(filedir));
    if proto==1
        cd(dir)
        cd ..
    end
end

cd(dir)
cd ..
cd ..
volunteerdir = pwd;

serie = str2num(volunteerdir(end-2:end));

% determine serienr
% dirnr1 = findstr('qmap_',volunteerdir);
% dirnr2 = findstr('test_',volunteerdir);
% if dirnr2-dirnr1 == 7
%     nr1 = str2num(volunteerdir(dirnr1+5));
%     nr2 = str2num(volunteerdir(dirnr1+12));
% elseif dirnr2-dirnr1 == 8
%     nr1 = str2num(volunteerdir(dirnr1+5:dirnr1+6));
%     nr2 = str2num(volunteerdir(dirnr1+13));
% end
% 
% if nr2==1
%     serie = (2*nr1)-1;
% else
%     serie = 2*nr1;
% end

% Set outputdir
outputdir=[volunteerdir,filesep,'Postprocessing',filesep,'Mapping_Masks', filesep];

% Try to load locations.mat for storage of image locations
cd ..
if nargin>3
    masknr=varargin{2};
    if exist('locations.mat')==2
        load('locations.mat')
    end
else
    masknr=1;

    if exist('locations.mat')==2
        load('locations.mat')
        if(size(locations,2)>=serie)
            masknr = size(locations(serie).loc,2)+1;
        end
    end
end


% Fill and save locations.mat
for proto=1:protocols
    location(proto,masknr).file = files(proto).file;
    location(proto,masknr).dir = files(proto).dir;
end

locations(serie).loc(:,masknr)=location(:,masknr);
save(['locations.mat'],'locations')


if exist(outputdir)~=7
   mkdir(outputdir);
end

% Determine Screensize for drawing
screensize = get(0,'Screensize');

if draw;
    for j=1:proto   
        for i=1:length(maskparts)

            if j~=1  % Show the SPGR with mask when drawing qmap masks
                figure('Name', ['Mask on McKinsey ' ], 'Position', [0.75*screensize(3),0.6*screensize(4),0.2*screensize(3),0.3*screensize(4)]);
                mr=drawdata(1).scan;            
                sc(McK(i).mask,[0,1],gray,sc(mr,[min(mr(:)),max(mr(:))],gray),McK(i).mask==0)%, jet,sc(MRIs(day).image, [0 2000],gray),(masks(day,2).image+3.*masks(day,1).image)<1,sc(spioedges(day).image,[1 1],gray),spioedges(day).image==1);
            end
            figure
            image = drawdata(j).scan;
            imshow(image,[1.1*min(image(:)) 0.9*max(image(:))],'InitialMagnification',100); %eventueel voor ] /1.5 zetten om beelden lichter te maken
            set(gcf, 'Position', [1,1,0.7*screensize(3),0.9*screensize(4)]);
            title(['Draw the',maskpartsnames{i},' region'])
            M=roipoly;
            if j==1
                McK(i).mask=M;
                cd(outputdir)         
            end
            close;
            if j~=1
                close;
            end
            save([outputdir,location(j,masknr).dir(end-8:end-1),'_', location(j,masknr).file(1:end-4), char(maskparts(i))],'M');
        end
    end
end

